package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.util.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);

    }

    @Override
    public void deleteById(Integer roleid) {
        roleMapper.deleteById(roleid);
    }

    @Override
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public List<Role> listRole() {
        return roleMapper.listRole();

    }

    @Override
    public PageResult<Role> page(String name, String label, Integer page, Integer pageSize) {
        //开启分页
        PageHelper.startPage(page,pageSize);
//        查询数据
        List<Role> list = roleMapper.list(name,label);
        Page<Role> rs =(Page<Role>) list;
        //对数据进行封装
       return PageResult.<Role>builder()
               .total((int)rs.getTotal())
               .rows(list)
               .build();

    }

    @Override
    public Role selectById(Integer roleid) {
        return   roleMapper.selectById(roleid);
    }
}
